package com.losdun.weixin.controller;

import com.losdun.common.annotation.Log;
import com.losdun.common.core.controller.BaseController;
import com.losdun.common.core.domain.AjaxResult;
import com.losdun.common.core.page.TableDataInfo;
import com.losdun.common.enums.BusinessType;
import com.losdun.common.utils.poi.ExcelUtil;
import com.losdun.weixin.domain.WechatUser;
import com.losdun.weixin.service.WechatUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 用户管理Controller
 * 
 * @author swordye
 * @date 2025-08-19
 */
@RestController
@RequestMapping("/weixin/user")
public class WechatUserController extends BaseController
{
    @Autowired
    private WechatUserService wechatUserService;

    /**
     * 查询用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('weixin:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(WechatUser wechatUser)
    {
        startPage();
        List<WechatUser> list = wechatUserService.selectWechatUserList(wechatUser);
        return getDataTable(list);
    }

    /**
     * 导出用户管理列表
     */
    @PreAuthorize("@ss.hasPermi('weixin:user:export')")
    @Log(title = "用户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WechatUser wechatUser)
    {
        List<WechatUser> list = wechatUserService.selectWechatUserList(wechatUser);
        ExcelUtil<WechatUser> util = new ExcelUtil<WechatUser>(WechatUser.class);
        util.exportExcel(response, list, "用户管理数据");
    }

    /**
     * 获取用户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('weixin:user:query')")
    @GetMapping(value = "/detail/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(wechatUserService.getById(id));
    }

    /**
     * 新增用户管理
     */
    @PreAuthorize("@ss.hasPermi('weixin:user:add')")
    @Log(title = "用户管理", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody WechatUser wechatUser)
    {
        return toAjax(wechatUserService.save(wechatUser));
    }

    /**
     * 修改用户管理
     */
    @PreAuthorize("@ss.hasPermi('weixin:user:edit')")
    @Log(title = "用户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody WechatUser wechatUser)
    {
        return toAjax(wechatUserService.updateById(wechatUser));
    }

    /**
     * 删除用户管理
     */
    @PreAuthorize("@ss.hasPermi('weixin:user:remove')")
    @Log(title = "用户管理", businessType = BusinessType.DELETE)
	@PostMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(wechatUserService.removeBatchByIds(Arrays.asList(ids)));
    }
}
